package com.tocean.app.service.common;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tocean.app.controller.request.datadict.DataDictOperateDto;
import com.tocean.app.controller.request.datadict.DataDictSearchDto;
import com.tocean.app.dto.response.BasePageResponse;
import com.tocean.app.entity.common.DataDict;
import com.tocean.app.mapper.common.DataDictMapper;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class DataDictService {

    @Autowired
    private DataDictMapper dataDictMapper;

    public BasePageResponse<DataDict> page(DataDictSearchDto params, Integer page, Integer size) {
        Page<DataDict> _page = new Page(page,size);
        IPage<DataDict> list = dataDictMapper.getList(_page,params);
        return new BasePageResponse().setTotal(list.getTotal()).setData(list.getRecords());
    }

    public List<DataDict> findDataDictById(String dataDictId) {
        List<DataDict> dataDicts = dataDictMapper.selectList(new QueryWrapper<DataDict>().eq("parent_id", dataDictId).eq("is_del", 0));
        return dataDicts;
    }

    public DataDict findDataDictByDictKey(String dictKey) {
        List<DataDict> dataDicts = dataDictMapper.selectList(new QueryWrapper<DataDict>().eq("dict_key", dictKey).eq("is_del", 0));

        if(!ObjectUtils.isEmpty(dataDicts)){
            return dataDictMapper.selectById(dataDicts.get(0).getId());
        }
        return null;
    }

    public void saveOrUpdate(DataDictOperateDto operateDto) {
        DataDict _dataDict;
        if (StringUtils.isEmpty(operateDto.getId())) {
            _dataDict = new DataDict();
            if (!StringUtils.isEmpty(operateDto.getParentId())) {
                _dataDict.setParentId(operateDto.getParentId());
            }
        } else {
            DataDict dataDict = dataDictMapper.selectById(operateDto.getId());
            _dataDict = dataDict;
        }

        _dataDict.setValue(operateDto.getValue());
        _dataDict.setAttr(operateDto.getAttr());
        _dataDict.setCode(operateDto.getCode());
        _dataDict.setRemarks(operateDto.getRemarks());
        _dataDict.setDictKey(operateDto.getDictKey());
        _dataDict.setSort(operateDto.getSort());

        if (StringUtils.isEmpty(operateDto.getId())) {
            dataDictMapper.insert(_dataDict);
        } else {
            dataDictMapper.updateById(_dataDict);
        }
    }

    @Transactional
    public void delete(String dataDictId) {
        List<DataDict> dataDicts = dataDictMapper.selectList(new QueryWrapper<DataDict>().eq("id", dataDictId).eq("is_del", 0));
        if (!ObjectUtils.isEmpty(dataDicts)) {
            List<DataDict> dataDictsByParentId = dataDictMapper.selectList(new QueryWrapper<DataDict>().eq("parent_id", dataDictId).eq("is_del", 0));
            if (dataDictsByParentId.size() != 0) {
                throw new RuntimeException(String.format("删除失败,[%s]还存在子级不能删除父级!", dataDictsByParentId.get(0).getDictKey()));
            }

            dataDictMapper.deleteById(dataDictId);
        }
    }

}
